package com.xmlit.project.engine.struct.impl;

import java.util.List;
//import java.util.regex.Pattern;

import com.xmlit.project.engine.struct.Struct;
import com.xmlit.project.engine.struct.StructChoice;

public class StructChoiceImpl extends StructImpl implements StructChoice {

	public StructChoiceImpl(){}
	public StructChoiceImpl(String name) {
		super(name);
	}

	public String calcRegex(CalcContext context, String delimiter) { 
		if (this.regex == null) {
			this.indexWithoutOccur = context.calcIndex;
			handleOccurIndex(context, delimiter);
			this.regexIndex = context.calcIndex;
			context.calcIndex++;
			/*if (!(this.getMinOccurrences() == 1 && this.getMaxOccurrences() == 1)) {
				context.calcIndex++;
				if (getParentDelimiter() != null) {
					context.calcIndex+=1;
				}

			}*/


			List<Struct> childern = getChildren();
			if (childern == null) {
				throw new RuntimeException(
						"StructSequenceImpl must have children");
			}
			StringBuffer res = new StringBuffer("(");
			if (getPrefix() != null)
				res.append(quote(getPrefix()));
			for (int i = 0; i < getChildren().size(); i++) {
				res.append(getChildren().get(i).calcRegex(context, null));
				if (i + 1 < getChildren().size()) {
					res.append("|");
				}
			}
			if (getSuffix() != null)
				res.append(quote(getSuffix()));
			res.append(")");
			this.regexPure = res.toString();
			res = handleOccur(res, context, delimiter);
			this.regex = res.toString();
		}

	/*	if (delimiter != null)
			regex = regex + delimiter;*/

		return regex;
	}

}
